package hz.mobileproject.pda.listener;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.xutils.common.Callback;

import hz.mobileproject.pda.utils.Base64;

/**
 * Created by Lc on 2017/11/22.
 */

public abstract class CustomNetCallBack implements Callback.CommonCallback<String> {
    @Override
    public void onSuccess(String result) {
        try{
            JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
            int rtnCode = Integer.valueOf(returnData.get("rtnCode").getAsString());
            String rtnMsg = returnData.get("rtnMsg").getAsString();
            String data = Base64.getFromBase64(returnData.get("data").getAsString());
            if(rtnCode == 0){
                onSuccessFromSer(rtnCode,rtnMsg,data);
            }else {
                LogUtils.e("返回code不为0，rtnCode = "+rtnCode+" , rtnMsg = "+rtnMsg);
                onErrorFromSer(rtnCode,rtnMsg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        onErrorFromNet(ex,isOnCallback);
    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }

    public abstract void onSuccessFromSer(int rtnCode, String rtnMsg, String data);
    public abstract void onErrorFromSer(int rtnCode, String rtnMsg);
    public abstract void onErrorFromNet(Throwable ex, boolean isOnCallback);
}
